package com.example.travelservlet.entertainment.controller;

import com.example.travelservlet.city.service.CityService;
import com.example.travelservlet.collection.service.CollectionService;
import com.example.travelservlet.collectionEntertainment.service.CollectionEntertainmentService;
import com.example.travelservlet.entertainment.service.EntertainmentService;
import com.example.travelservlet.entertainment.service.EntertainmentService1;
import com.example.travelservlet.entertainmentPlan.service.EntertainmentPlanService;
import com.example.travelservlet.entity.*;
import com.example.travelservlet.entertainmentLabel.service.EntertainmentLabelService;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.user.domain.UserResult;
import com.example.travelservlet.user.exception.BizException;
import com.example.travelservlet.user.service.UserService;
import com.example.travelservlet.userLabel.service.UserLabelService;
import com.example.travelservlet.utils.ResultUtil;
import com.example.travelservlet.utils.ResultUtilPlus1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hxy
 * @date 2022/11/9 8:27
 */
@RestController
@RequestMapping("/entertainment")
public class EntertainmentController {
    @Autowired
    private EntertainmentService entertainmentService;
    @Autowired
    private EntertainmentService1 entertainmentService1;
    @Autowired
    private UserService userService;
    @Autowired
    private CityService cityService;
    @Autowired
    private UserLabelService userLabelService;
    @Autowired
    private EntertainmentLabelService entertainmentLabelService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private CollectionEntertainmentService collectionEntertainmentService;

    @Autowired
    private EntertainmentPlanService entertainmentPlanService;



    @GetMapping("/findEntertainmentResearch/{cityId}/{str}")
    public Result<List<Entertainment>> findEntertainmentResearch(@PathVariable String cityId,@PathVariable String str,
        @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
        @RequestParam(name = "size", required = false, defaultValue = "10") Integer size){
        Pageable pageable = PageRequest.of(page - 1, size);
        Page research = entertainmentService.findResearch(cityId, pageable, str);

        return ResultUtil.success(research);
    }

    /**
     * 通过标签查找景点美食
     * @param loginName
     * @param type
     * @param entertainment
     * @param page
     * @param size
     * @return
     */

    @GetMapping("/findEntertainment/{loginName}/{type}")
    public Result<List<Entertainment>> findEntertainment(@PathVariable String loginName, @PathVariable String type, Entertainment entertainment,
                                                         @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                         @RequestParam(name = "size", required = false, defaultValue = "10") Integer size) {

        User user = userService.findUserByLoginName(loginName);
        City city = cityService.findCityById(user.getCityId());

        List<Entertainment> entertainments = entertainmentService1.findEntertainmentsByCityIdAndEntertainmentIds1(loginName,user.getCityId(),type,page,size);
        System.out.println(entertainments);
        return ResultUtil.success(entertainments);
    }

    /**
     * 在收藏中展示景点美食
     * @param loginName
     * @param entertainment
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findCollectedEntertainment/{loginName}")
    public Result<List<Entertainment>> findCollectedEntertainment(@PathVariable String loginName, Entertainment entertainment,
                                                         @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                         @RequestParam(name = "size", required = false, defaultValue = "10") Integer size) {


        User user = userService.findUserByLoginName(loginName);
        List<Collection> collections = collectionService.findCollectionsByUserId(user.getUserId());
        List<String> entertainmentIds = collectionEntertainmentService.findEntertainmentIdsByCollectionId(collections.get(0).getCollectionId());
        if (page < 1) {
            throw new BizException("[查询所有项目]:当前页码不能小于1");
        }
        Pageable pageable = PageRequest.of(page - 1, size);
        Page pageResult = entertainmentService.findEntertainmentsByEntertainmentIds(entertainmentIds, pageable);
        return ResultUtil.success(pageResult);
    }

    @GetMapping("/findEntertainmentById/{entertainmentId}")
    public Result<Entertainment> findEntertainmentById(@PathVariable String entertainmentId,Entertainment entertainment){
        return ResultUtil.success(entertainmentService.findEntertainmentByEntertainmentId(entertainmentId));
    }

    @GetMapping("/findEntertainmentsByPlanId/{planId}")
    public Result findEntertainmentsByPlanId(@PathVariable String planId){
        List<EntertainmentPlan> entertainmentPlans = entertainmentPlanService.findEntertainmentPlansByPlanId(planId);
        List<String> entertainmentIds = new ArrayList<>();
        for (EntertainmentPlan entertainmentPlan : entertainmentPlans) {
            entertainmentIds.add(entertainmentPlan.getEntertainmentId());
        }
        List<Entertainment> entertainments = new ArrayList<>();
        for (String entertainmentId:entertainmentIds){
            Entertainment entertainment = new Entertainment();
            entertainment = entertainmentService.findEntertainmentByEntertainmentId(entertainmentId);
            entertainments.add(entertainment);
        }
       return ResultUtil.success(entertainments);
    }

    @GetMapping("findEntertainmentsByCityId/{cityId}")
    public Result<List<Entertainment>> findEntertainmentsByCityId(@PathVariable String cityId,Entertainment entertainment) {
        return ResultUtil.success(entertainmentService.findEntertainmentsByCityIdAndType(cityId,"scenery"));
    }
    @PostMapping("/findEntertainmentsByEntertainmentIds")
    public Result<List<Entertainment>> findEntertainmentsByEntertainmentIds(@RequestParam @Valid List<String> entertainmentIds){
//        List<String> entertainmentIds = new ArrayList<>();
        List<Entertainment> entertainments = new ArrayList<>();
        for (String entertainmentId : entertainmentIds){
            Entertainment entertainment = entertainmentService.findEntertainmentByEntertainmentId(entertainmentId);
            entertainments.add(entertainment);
        }
        return ResultUtil.success(entertainments);
    }

    @PostMapping("/add")
    public Entertainment addEntertainment(Entertainment entertainment){
        return entertainmentService.saveEntertainment(entertainment);
    }

    @PostMapping("/addScenery")
    public Entertainment addScenery(@Valid String name,@Valid String description,@Valid String picture1,@Valid String picture2,@Valid String picture3,@Valid String city,@Valid Integer time){
        Entertainment entertainment = new Entertainment();
        int size = entertainmentService.findAlls().size();
        entertainment.setEntertainmentId(String.valueOf(size+1));
        entertainment.setName(name);
        entertainment.setDescription(description);
        entertainment.setPicture1(picture1);
        entertainment.setPicture2(picture2);
        entertainment.setPicture3(picture3);
        City cityByName = cityService.findCityByName(city);
        entertainment.setCityId(cityByName.getCityId());
        entertainment.setType("scenery");
        entertainment.setTime(time);
        return entertainmentService.saveEntertainment(entertainment);
    }
    @PostMapping("/addFood")
    public Entertainment addFood(@Valid String name,@Valid String description,@Valid String picture1,@Valid String picture2,@Valid String picture3,@Valid String city,@Valid Integer time){
        Entertainment entertainment = new Entertainment();
        int size = entertainmentService.findAlls().size();
        entertainment.setEntertainmentId(String.valueOf(size+1));
        entertainment.setName(name);
        entertainment.setDescription(description);
        entertainment.setPicture1(picture1);
        entertainment.setPicture2(picture2);
        entertainment.setPicture3(picture3);
        City cityByName = cityService.findCityByName(city);
        entertainment.setCityId(cityByName.getCityId());
        entertainment.setType("food");
        entertainment.setTime(time);
        return entertainmentService.saveEntertainment(entertainment);
    }

    @GetMapping("/delete")
    public void deleteEntertainment(Entertainment entertainment){
        entertainmentService.deleteEntertainment(entertainment);
    }

    @GetMapping("/findSceneries")
    @ResponseBody
    public UserResult<List<Entertainment>> findSceneries(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                         @RequestParam(name = "size", required = false, defaultValue = "10") Integer size){
        System.out.print(System.currentTimeMillis()+"$11111");
        List<Entertainment> sceneries= entertainmentService.findSceneries();

        System.out.print(System.currentTimeMillis()+"$222222");

        List<Entertainment> sceneries1 = entertainmentService.findSceneries1(page, size);
        return ResultUtilPlus1.success(sceneries1,sceneries.size());
    }
    @GetMapping("/findSceneriesByKeyWord/{key}")
    public UserResult<List<Entertainment>> findSceneriesByKeyWord(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                                  @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,@PathVariable String key){
        List<Entertainment> sceneries= entertainmentService.findSceneriesByKeyWord(key);
        List<Entertainment> sceneries1 = entertainmentService.findSceneriesByKeyWord(key,page,size);
        return ResultUtilPlus1.success(sceneries1,sceneries.size());

    }

    @GetMapping("/findFoodsByKeyWord/{key}")
    public UserResult<List<Entertainment>> findFoodsByKeyWord(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                              @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,@PathVariable String key){
        List<Entertainment> foods= entertainmentService.findFoodsByKeyWord(key);
        List<Entertainment> foods1 = entertainmentService.findFoodsByKeyWord1(key,page,size);
        return ResultUtilPlus1.success(foods1,foods.size());
    }

    @GetMapping("/findFoods")
    @ResponseBody
    public UserResult<List<Entertainment>> findFoods(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                     @RequestParam(name = "size", required = false, defaultValue = "10") Integer size){
        List<Entertainment> foods= entertainmentService.findFoods();
//        Pageable pageable = PageRequest.of(page - 1, size);
//        Page pageResult = entertainmentService.findFoods1( pageable);
        List<Entertainment> foods1 = entertainmentService.findFoods1(page, size);
        return ResultUtilPlus1.success(foods1,foods.size());
    }


    @PostMapping("/update")
    public Result<Entertainment> update(@Valid String entertainmentId,@Valid String name,@Valid String description,@Valid String picture1,@Valid String picture2,@Valid String picture3,@Valid String city,@Valid Integer time) {
        Entertainment entertainment = entertainmentService.findEntertainmentByEntertainmentId(entertainmentId);
        City cityByName = cityService.findCityByName(city);
        entertainment.setName(name);
        entertainment.setDescription(description);
        entertainment.setPicture1(picture1);
        entertainment.setPicture2(picture2);
        entertainment.setPicture3(picture3);
        entertainment.setCityId(cityByName.getCityId());
        entertainment.setTime(time);

        entertainmentService.updateEntertainment(entertainment);
        return ResultUtil.success(entertainment);
    }


    @GetMapping("/deleteById/{entertainmentId}")
    public Result deleteById(@PathVariable String entertainmentId) {
        entertainmentService.deleteById(entertainmentId);
        return ResultUtil.success(null);
    }


}